<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

require_once ROOT_PATH.'include/sql/function/story.sql.php';
require_once ROOT_PATH.'service/base.service.php';
require_once ROOT_PATH.'service/user.service.php';
require_once ROOT_PATH.'service/tag.service.php';
require_once ROOT_PATH.'service/feed.service.php';
require_once ROOT_PATH.'include/logic/story.logic.php';
require_once ROOT_PATH.'include/logic/image.logic.php';

class StoryService extends BaseService
{
	private $sqlFunc;
	private $userService;
	private $feedService;
	
	function __construct()
	{
		parent::__construct();
		
		$this->sqlFunc = new StorySqlFunc();
		$this->userService = new UserService();
		$this->feedService = new FeedService();
	}
	
	/**
	 *
	 * 获取故事信息
	 * @param String $id
	 */
	function get_last_add($uid)
	{
		$result = $this->sqlFunc->get_last_add($uid);
		return $result;
	}
	
	/**
	 *
	 * 获取故事信息
	 * @param String $id
	 */
	function get($uid, $id)
	{
		$result = $this->sqlFunc->get($uid, $id);
		if (check_array($result)) {
			$result['image1'] = get_resource_url($result['image1']);
			$result['image2'] = get_resource_url($result['image2']);
			$result['image3'] = get_resource_url($result['image3']);
			$result['create_time'] = format_time($result['create_time']);
			
			$result['is_follow'] = $this->userService->check_follow(array('uid'=>$uid, 'id'=>$result['user_id']));
			$result['last_like'] = $this->sqlFunc->get_like_list(array('id'=>$id, 'page_size'=>6, 'page_index'=>0));
			$comment_list = $this->get_comment_list(array('sid'=>$id, 'page_size'=>5, 'page_index'=>0));
			$result['comment_list'] = isset($comment_list['list']) ? $comment_list['list'] : null;
		}
		return $result;
	}

	/**
	 * 
	 * 获取最新故事列表
	 * @param array $request
	 */
	function get_new_list($request)
	{
		$result = $this->sqlFunc->get_new_list($request);
		$this->format_story_list($result);
		return $result;
	}
	
	/**
	 * 
	 * 获取热门故事列表
	 * @param array $request
	 */
	function get_hot_list($request)
	{
		$result = $this->sqlFunc->get_hot_list($request);
		$this->format_story_list($result);
		return $result;
	}
	
	/**
	 * 
	 * 根据uid获取故事列表
	 * @param array $request
	 */
	function get_list_by_follow($request)
	{
		$result = $this->sqlFunc->get_list_by_follow($request);
		$this->format_story_list($result);
		return $result;
	}
	
	/**
	 * 
	 * 根据uid获取故事列表
	 * @param array $request
	 */
	function get_list_by_uid($request)
	{
		$result = $this->sqlFunc->get_list_by_uid($request);
		$this->format_story_list($result);
		return $result;
	}
	
	function get_list_by_tag($request)
	{
		$result = $this->sqlFunc->get_list_by_tag($request);
		$this->format_story_list($result);
		return $result;
	}

	/**
	 *
	 * 搜索故事
	 * @param array $request
	 */
	function search($request)
	{
		$result = $this->sqlFunc->search($request);
		$this->format_story_list($result);
		return $result;
	}
	
	function format_story_list(&$result)
	{
		if (isset($result['list']) && check_array($result['list']))
		{
			foreach ($result['list'] as &$arr)
			{
				if (isset($arr['create_time']))
				{
					$arr['image1'] = get_resource_url($arr['image1']);
					$arr['image2'] = get_resource_url($arr['image2']);
					$arr['image3'] = get_resource_url($arr['image3']);
					$arr['create_time'] = format_time($arr['create_time']);
				}
			}
		}
	}

	/**
	 *
	 * 新增故事
	 * @param array $request
	 */
	function add($request)
	{
		//var_dump('---Story info before save image---', $request);
		
		$image1 = isset($request['image1']) ? $request['image1'] : '';
		$request['image1'] = '';
		$image2 = isset($request['image2']) ? $request['image2'] : '';
		$request['image2'] = '';
		$image3 = isset($request['image3']) ? $request['image3'] : '';
		$request['image3'] = '';
		
		$time = time();
		$rd = rand(1, 99);
		if (stripos($image1, 'http') !== false)
		{
			$request['image1'] = $image1;
		}
		else if($image1)
		{
			//$pkg_name = 'story-'.$rd;
			$img_name = '/story/'.$rd.'/'.$rd.'-'.$time.'1.jpg';
			$url = save_image($img_name, $image1);
			if ($url != '') $request['image1'] = $url;
		}
		
		if (stripos($image2, 'http') !== false)
		{
			$request['image2'] = $image2;
		}
		else if($image2)
		{
			//$pkg_name = 'story-'.$rd;
			$img_name = '/story/'.$rd.'/'.$rd.'-'.$time.'2.jpg';
			$url = save_image($img_name, $image2);
			if ($url != '') $request['image2'] = $url;
		}
		
		if (stripos($image3, 'http') !== false)
		{
			$request['image3'] = $image3;
		}
		else if($image3)
		{
			//$pkg_name = 'story-'.$rd;
			$img_name = '/story/'.$rd.'/'.$rd.'-'.$time.'3.jpg';
			$url = save_image($img_name, $image3);
			if ($url != '') $request['image3'] = $url;
		}
		
		if ($request['url'] && stripos($request['url'], 'oye-bucket') > 0)
		{
			$input_name = substr($request['url'], stripos($request['url'], 'oye-bucket')+11);
			$output_name = 'media/userVideo/'.date('Y-m').'/'.substr($request['url'], strripos($request['url'], '/')+1);
			
			$result = trans_video($input_name, $output_name);
			
			//var_dump($input_name, $output_name, $result);
			
			if ($result) $request['url'] = 'http://bcs.duapp.com/oye-bucket/'.$output_name;
		}
		
		$logic = new StoryLogic();
		$logic->add_story($request);

		//var_dump('---Story info---', $request);
		
		$result = $this->sqlFunc->add($request);
		
		if ($result)
		{
			$this->userService->update_story_count($request['uid']);
			//$request['type'] = 's';
			//$this->feedService->add($request);
		}

		return $request;
	}

	/**
	 *
	 * 删除故事
	 * @param array $ids
	 */
	function delete($request) 
	{
		$info = $this->get($request['uid'], $request['id']);
		
		$result = false;
		if (check_array($info))
		{
			$result = $this->sqlFunc->delete($request['uid'], $request['id']);
			$this->userService->update_story_count($request['uid']);
			
			$obj_name = $info['url'];
			if (strlen($obj_name) > 10 && stripos($obj_name, 'oye-bucket') > 0)
			{
				$obj_name = substr($obj_name, stripos($obj_name, 'oye-bucket')+10);
				delete_from_bcs($obj_name);
			}
		}
		
		
		return $result;
	}
	
	/**
	 *
	 * 更新故事信息
	 * @param array $request
	 */
	function update($request)
	{
		$info = $this->get($request['uid'], $request['id']);

		$result = false;
		if (check_array($info))
		{
			foreach ($info as $k=>$v)
			{
				if ($v != '' && array_key_exists($k, $info)) $info[$k] = $v;
			}
				
			$image1 = isset($request['image1']) ? $request['image1'] : '';
			$request['image1'] = '';
			$image2 = isset($request['image2']) ? $request['image2'] : '';
			$request['image2'] = '';
			$image3 = isset($request['image3']) ? $request['image3'] : '';
			$request['image3'] = '';

			// TODO, delete old image
			$rd = rand(1, 99);
			if($image1)
			{
				//$pkg_name = 'story-'.$rd;
				$img_name = '/story/'.$rd.'/'.$rd.'-'.$time.'1.jpg';
				$url = save_image($img_name, $image1);
				if ($url != '') {
					if ($info['image1'] != '') delete_from_bcs($info['image1']);
					$info['image1'] = $url;	
				}
			}
			if($image2)
			{
				//$pkg_name = 'story-'.$rd;
				$img_name = '/story/'.$rd.'/'.$rd.'-'.$time.'2.jpg';
				$url = save_image($img_name, $image2);
				if ($url != '') {
					if ($info['image2'] != '') delete_from_bcs($info['image2']);
					$info['image2'] = $url;
				}
			}
			if($image3)
			{
				//$pkg_name = 'story-'.$rd;
				$img_name = '/story/'.$rd.'/'.$rd.'-'.$time.'3.jpg';
				$url = save_image($img_name, $image3);
				if ($url != '') {
					if ($info['image3'] != '') delete_from_bcs($info['image3']);
					$info['image3'] = $url;
				}
			}
			
			if (isset($_FILES["file"]))
			{
				$arr = explode('.', $_FILES['name']);
				$file_name = '/story/'.$rd.'/'.$rd.'-'.$time.'.'.(check_array($arr) ? $arr[count($arr)-1] : 'mp4');
				$url = save_to_bcs_from_file($file_name, $_FILES["file"]["tmp_name"]);
				if ($url != '') {
					if ($info['url'] != '') delete_from_bcs($info['url']);
					$info['url'] = $url;
				}
			}
				
			$result = $this->sqlFunc->update($info);
		}

		return $result;
	}

	/**
	 * TODO, 新增用户动态
	 *
	 * 新增故事like
	 * @param array $request
	 */
	function add_like($request)
	{
		$this->feedService->add(array('uid'=>$request['uid'],'fsid'=>$request['sid'],'type'=>'l', 'time'=>time()));
		
		$result = $this->sqlFunc->add_like($request);
		if ($result)
		{
			$this->sqlFunc->update_like_count($request['sid']);
		}
		
		return $result;
	}
	
	function check_like($request)
	{		
		return $this->sqlFunc->check_like($request);
	}

	/**
	 *
	 * 故事like列表
	 * @param array $request
	 */
	function get_like_list($request)
	{
		return $this->sqlFunc->get_like_list($request);
	}

	/**
	 *
	 * 新增故事评论
	 * @param array $request
	 */
	function add_comment($request)
	{
		$logic = new StoryLogic();
		$logic->add_comment($request);
		
		$this->feedService->add(array('uid'=>$request['uid'],'fsid'=>$request['sid'],'type'=>'c', 'time'=>time()));
		
		$result = $this->sqlFunc->add_comment($request);
		if ($result)
		{
			$this->sqlFunc->update_comment_count($request['sid']);
		}
		
		return $result;
	}

	function check_comment($request)
	{		
		return $this->sqlFunc->check_comment($request);
	}
	
	/**
	 *
	 * 故事评论列表
	 * @param array $request
	 */
	function get_comment_list($request)
	{
		$result = $this->sqlFunc->get_comment_list($request);
		
		if (isset($result['list']) && check_array($result['list']))
		{
			foreach ($result['list'] as &$arr)
			{
				if (isset($arr['create_time']))
				{
					$arr['create_time'] = format_time($arr['create_time']);
				}
			}
		}
		
		return $result;
	}
	
	function clear_order()
	{
		return $this->sqlFunc->clear_order();
	}
	
	function set_order($request)
	{
		return $this->sqlFunc->set_order($request);
	}
}